import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../layout/Layout.vue'
import Login from '../views/Login.vue'
import About from '../views/About.vue'
import Backstage from '../views/backstage/Backstage.vue'
import Parkingrecord from '../views/parkingrecord/Parkingrecord.vue'
import Charges from '../views/charges/Charges.vue'
import Policeofficer from '../views/policeofficer/Policeofficer.vue'
import Alertlist from '../views/alertlist/Alertlist.vue'
import Historyalert from '../views/historyalert/Historyalert.vue'
import Trafficuidex from '../views/trafficuidex/Trafficuidex.vue'
import Register from '../views/Register.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/backstage'
  },
  {
    path: '/layout', //布局路由
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: 'backstage', //首页
        name: 'backstage',
        component: Backstage
      },
      {
        path: 'parkingrecord', //停车记录
        name: 'parkingrecord',
        component: Parkingrecord
      },
      {
        path: 'charges', //收费记录
        name: 'charges',
        component: Charges
      },
      {
        path: 'policeofficer', //警员列表
        name: 'policeofficer',
        component: Policeofficer
      },
      {
        path: 'alertlist', //警情列表
        name: 'alertlist',
        component: Alertlist
      },
      {
        path: 'historyalert', //历史警情
        name: 'historyalert',
        component: Historyalert
      },
      {
        path: 'trafficuidex', //交通指数
        name: 'trafficuidex',
        component: Trafficuidex
      },
      {
        path: 'about',
        name: 'about',
        component: About
      }
    ]
  },
  {
    path: '/login', //登陆
    name: 'login',
    component: Login
  },
  {
    path:'/register',
    name:'register',
    component:Register
  }
]

const router = new VueRouter({
  routes
})

import {getToken} from '../utls/cookieUtils'
import {getUserApi} from '../api/loginApi'
import store from '../store/index'
import Shenpi from '../views/Shenpi.vue'
import Zichan from '../views/Zichan.vue'
const routeAsync = [
  {
    path:'shenpi',
    name:'Shenpi',
    component:Shenpi,
    meta:{ title:'考勤管理',rules:['管理员']}
  },
  {
    path:'zichan',
    name:'Zichan',
    component:Zichan,
    meta:{ title:'资产管理',rules:['用户','管理员']}
  }
]
function getOwnRuleMenu(rule,routeAsync){
  var arr = []
  routeAsync.forEach(item=>{
    if(item.meta.rules.includes(rule)){
      arr.push(item)
    }
  })
  return arr
}
var whiteArr = ['/login','/register']
router.beforeEach((to,from,next)=>{
  var token = getToken('token')
  if(token){ 
      if(to.path == '/login'){
         next('/')
      }else{
        if(store.state.routes.length == 0){
             console.log('token',token);
            getUserApi({token:token}).then(res=>{
              console.log('用户信息',res);
              if(res.code == 200){
                  var rule = res.data.rule
                  store.commit('getuser',res.data) 
                  var newRoue =  getOwnRuleMenu(rule,routeAsync)
                  var myRoutes = routes
                  myRoutes[1].children = routes[1].children.concat(newRoue)
                  console.log('routes',myRoutes);
                  store.commit('getroute',myRoutes)
                  router.addRoutes(routes) 
                  next({...to,replace:true})
                }
            })
        }else{
          next()
        }
      }
  }else{//没有token
     if(whiteArr.includes(to.path)){
        next()
     }else{
       next('/login')
     }
  }
})

export default router
